<?php namespace App\Services;

use Sentinel;
use App\Exceptions\ValidateParameterException;
use Cartalyst\Sentinel\Checkpoints\ThrottlingException;


class LoginService{

	public function toLogin($data,$remember){
		
		try {
			$user = Sentinel::authenticate($data);

			if(!$user){
				throw new ValidateParameterException('账号或者密码不正确');
			}

			Sentinel::login($user,$remember);
		} catch (ThrottlingException $e) {

			$min = floor($e->getDelay()/60);

			throw new ValidateParameterException("登录失败次数超过限制，请{$min}分钟后再登录");
		}
	}

	public function logout(){
		$user = Sentinel::getUser();

		Sentinel::logout($user);
	}
}